import { apiLyric, checkMusic, playlistDetail } from '@/api'
import { Notification } from 'element-ui';
let mes = null;
export default {
  namespaced: true,
  state: {
    musicUrl: [],
    musicControlShow: false
  },
  mutations: {
    CHANGE_MUSIC_URL(state, data) {
      let arr = JSON.parse(JSON.stringify(state.musicUrl))
      arr.push(data)
      let hash = {};
      let newArr = arr.reduce((preVal, curVal) => {
        hash[curVal.id] ? "" : (hash[curVal.id] = true && preVal.push(curVal));
        return preVal;
      }, []);
      state.musicUrl = newArr
    },
    CHANGE_MUSIC_CONTROL_SHOW(state) {
      state.musicControlShow = !state.musicControlShow
    }
  },
  actions: {
    SET_CHANGE_MUSIC_URL({ commit }, data) {
      commit("CHANGE_MUSIC_URL", data)
    },
    SET_CHANGE_MUSIC_CONTROL_SHOW({ commit }) {
      commit("CHANGE_MUSIC_CONTROL_SHOW")
    },
    // 音乐播放 
    async AUDIO_MUSIC_FORMAT(context, message) {
      let val = JSON.parse(JSON.stringify(message))
      await apiLyric({ id: val.id }).then(data => {
        val.lrc = data.lrc.lyric
      })
      const { id, name, lrc } = val;
      const { picUrl: cover } = val.al;
      const { name: artist } = val.ar[0]

      const config = {
        lrc,
        id,
        name,
        artist,
        cover,
        url: `https://music.163.com/song/media/outer/url?id=${id}.mp3`
      }
      let musicFlag = false;
      await checkMusic({ id }).then((data) => {
        musicFlag = data.success;
      });
      return {
        musicFlag,
        config
      }
    },
    // 专辑播放
    async ALBUM_MUSIC_LIST_FORMAT(context, data) {
      mes ? mes.close() : ''
      // 获取专辑详情
      let { playlist } = await playlistDetail({ id: data.id })
      mes = Notification({
        title: '提示',
        type: "warning",
        message: '正在加载' + playlist.trackIds.length + "首歌曲",
        duration: 0
      });
      let config = [];
      let musicFlag = false;
      await checkMusic({ id: playlist.trackIds.map(item => item.id).join(",") }).then((data1) => {
        musicFlag = data1.success;
      });
      for (let i = 0; i < playlist.tracks.length; i++) {
        await apiLyric({ id: playlist.tracks[i].id }).then(data1 => {
          playlist.tracks[i].lrc = data1.lrc ? data1.lrc.lyric : '暂无歌词'
        })
      }
      playlist.tracks.map(item => {
        let { id, name, lrc } = item;
        let { picUrl: cover } = item.al;
        let { name: artist } = item.ar[0]
        config.push({
          lrc,
          id,
          name,
          artist,
          cover,
          url: `https://music.163.com/song/media/outer/url?id=${id}.mp3`
        })
      })
      mes.close()
      return {
        config, musicFlag
      }
      // let {config,musicFlag} = await this.ALBUM_MUSIC_LIST_FORMAT(val)
      // if (musicFlag) {
      //   config.map(async item=>{
      //     await this.SET_CHANGE_MUSIC_URL(item)
      //   })
      //   this.$Notification("添加成功!","success");
      // } else {
      //   this.$Notification("此歌曲暂无版权。");
      // }
    }
  }
}
